<template>
  <div id="app">
    <el-container class="layout-container">
      <el-aside width="200px" class="sidebar">
        <div class="logo">
          <h2>API自动化平台</h2>
        </div>
        <el-menu
          :default-active="$route.path"
          class="sidebar-menu"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/dashboard">
            <el-icon><DataBoard /></el-icon>
            <span>仪表盘</span>
          </el-menu-item>
          <el-menu-item index="/environments">
            <el-icon><Setting /></el-icon>
            <span>环境管理</span>
          </el-menu-item>
          <el-menu-item index="/testcases">
            <el-icon><Document /></el-icon>
            <span>测试用例</span>
          </el-menu-item>
          <el-menu-item index="/executions">
            <el-icon><VideoPlay /></el-icon>
            <span>执行记录</span>
          </el-menu-item>
          <el-menu-item index="/reports">
            <el-icon><PieChart /></el-icon>
            <span>测试报告</span>
          </el-menu-item>
          <el-menu-item index="/daily/log">
            <el-icon><Document /></el-icon>
            <span>日报日志</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-container>
        <el-header class="header">
          <div class="header-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item v-for="item in breadcrumbs" :key="item.path" :to="item.path">
                {{ item.name }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div class="header-right">
            <el-dropdown>
              <span class="user-info">
                <el-avatar size="small" :src="userAvatar" />
                <span>{{ userName }}</span>
                <el-icon><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>个人设置</el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { logout } from '@/api'
import { ElMessage } from 'element-plus'

export default {
  name: 'App',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const userAvatar = ref('')
    const userName = ref('管理员')
    
    // 从localStorage获取用户信息
    const user = JSON.parse(localStorage.getItem('user') || 'null')
    if (user && user.username) {
      userName.value = user.username
    }

    const breadcrumbs = computed(() => {
      const paths = route.path.split('/').filter(Boolean)
      const breadcrumbItems = [{ path: '/', name: '首页' }]
      
      let currentPath = ''
      paths.forEach(path => {
        currentPath += `/${path}`
        const name = getBreadcrumbName(path)
        breadcrumbItems.push({ path: currentPath, name })
      })
      
      return breadcrumbItems
    })

    const getBreadcrumbName = (path) => {
      const nameMap = {
        dashboard: '仪表盘',
        environments: '环境管理',
        testcases: '测试用例',
        executions: '执行记录',
        reports: '测试报告'
      }
      return nameMap[path] || path
    }

    const handleLogout = () => {
      logout()
      ElMessage.success('已退出登录')
      router.push('/login')
    }

    return {
      userAvatar,
      userName,
      breadcrumbs,
      handleLogout
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.layout-container {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  color: #bfcbd9;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  border-bottom: 1px solid #434a50;
}

.logo h2 {
  font-size: 18px;
  font-weight: 600;
}

.sidebar-menu {
  border-right: none;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
}
</style> 